package com.oyjd.fw.tool.util;

import java.io.File;

import com.oyjd.fw.util.FileOP;

/**
 * 生成本工程的一个副本
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class BornNewProject {
	private static String[] filterFile = { "jsp", "jpg", "png", "gif", "js", "css" };
	private static String encoding = "utf-8";
	private static String projectOldPackage = "com.creawor";
	private static String projectOldName = "sxfoss";
	private static String souPath = "E:\\sxFoss\\server";

	private static String projectNewPackage = "com.creawor";
	private static String projectNewName = "CustTag";
	private static String outPath = "E:/Tag/Trunk/04code/";

	private static String[] filters = { ".svn", "target", "logs" };

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		doLoop(new File(souPath));
	}

	public static void doLoop(File dir) throws Exception {
		File[] ff = dir.listFiles();
		for (File f : ff) {
			if (f.isDirectory()) {
				boolean flag = true;
				for (String filter : filters) {
					if (f.getPath().indexOf(filter) != -1) {
						flag = false;
						break;
					}
				}
				if (flag) {
					doLoop(f);
				}
			} else if (f.isFile()) {
				String outFile = getOutFilePath(f.getPath());
				if (isFilterFile(f.getName())) {
					byte[] b = FileOP.readFile(f.getPath());
					FileOP.writeFile(outFile, b);
				} else {
					String c = FileOP.readFile(f.getPath(), encoding);
					c = c.replaceAll(projectOldPackage, projectNewPackage);
					c = c.replaceAll(projectOldName, projectNewName);
					FileOP.writeFile(outFile, c, encoding);
				}
				System.out.println("--->" + outFile);
			}
		}
	}

	private static boolean isFilterFile(String name) {
		boolean flag = false;
		for (String s : filterFile) {
			if (name.endsWith(s)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	private static String getOutFilePath(String path) {
		String p = path.replace(souPath, outPath);
		p = p.replace(projectOldPackage.replaceAll("\\.", "\\\\"), projectNewPackage.replaceAll("\\.", "\\\\"));
		return p;
	}

}
